package com.wjx.controller;


import com.wjx.dtos.ResponseResult;
import com.wjx.dtos.UserDTO;
import com.wjx.pojo.Deposits;
import com.wjx.pojo.Withdrawals;
import com.wjx.service.DepositsService;
import com.wjx.service.WithdrawalsService;
import com.wjx.utils.ThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/savings")
@RequiredArgsConstructor
public class SaveController {
    @Autowired
    private final DepositsService depositService;
    @Autowired
    private final WithdrawalsService withdrawalService;

    // 存款接口
    @PostMapping("/deposit")
    public ResponseResult deposit(@RequestBody UserDTO userDTO) {
        Deposits deposits = new Deposits();
        BeanUtils.copyProperties(userDTO, deposits);
        ThreadLocalUtil.setUser(userDTO);
        return depositService.savemoney(deposits);
    }
     //取款接口
    @PostMapping("/withdraw")
    public ResponseResult withdraw(@RequestBody UserDTO userDTO) {
        Withdrawals withdrawals = new Withdrawals();
        BeanUtils.copyProperties(userDTO, withdrawals);
        ThreadLocalUtil.setUser(userDTO);
        return withdrawalService.drawmoney(withdrawals);
    }

}
